package com.LostAndFound.web.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.LostAndFound.service.UserHomeService;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class UserHomeServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;

	UserHomeService service = new UserHomeService();
	
	// 得到所有记录
	public void getAllSearchLoseRecords(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String whatFlag = request.getParameter("whatFlag");
		List<Map<String, Object>> mapList = null;
		mapList = service.getAllSearchLoseRecords(whatFlag);
		
		Gson gson = new Gson();
		String json = gson.toJson(mapList);
		// 遍历json数组
		JsonParser parser = new JsonParser();
		JsonElement jsonElement = parser.parse(json);
		JsonArray jsonArray = jsonElement.getAsJsonArray();
		for (int i = 0; i < jsonArray.size(); i++) {
			JsonObject jsonObject = jsonArray.get(i).getAsJsonObject();
			// 截取title，拼接
			String titleStr = "";
			if(jsonObject.get("title").getAsString().length()>8) {
				titleStr = jsonObject.get("title").getAsString().substring(0, 8)+"......";
			}else {
				titleStr = jsonObject.get("title").getAsString();
			}
			jsonObject.addProperty("titleStr", titleStr);
			// 截取detailinfo，拼接
			String detailinfoStr = "";
			if(jsonObject.get("detailinfo").getAsString().length()>12) {
				detailinfoStr = jsonObject.get("detailinfo").getAsString().substring(0, 12)+"......";
			}else {
				detailinfoStr = jsonObject.get("detailinfo").getAsString();
			}
			jsonObject.addProperty("detailinfoStr", detailinfoStr);
			// 截取issuetime，拼接
			String issuetimeStr = "";
			if(jsonObject.get("issuetime").getAsString().length()>10) {
				issuetimeStr = jsonObject.get("issuetime").getAsString().substring(0, 10);
			}else {
				issuetimeStr = jsonObject.get("issuetime").getAsString();
			}
			jsonObject.addProperty("issuetimeStr", issuetimeStr);
		}
		// 重新格式化json数据		
		String rows = gson.toJson(jsonElement);
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write("{\"articleList\":"+rows+"}");
	}
	// 展示记录详情
	public void showArticleDetailByAid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String aid = request.getParameter("aid").trim();
		Map<String, Object> article = null;
		article = service.showArticleDetailByAid(aid);
		HttpSession session = request.getSession();
		session.setAttribute("articleDetail", article);
		response.sendRedirect(request.getContextPath()+"/user/01/detail.jsp");
	}
	// 搜素进行展示记录列表
	public void getMySearchRecord(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String whatFlag = request.getParameter("whatFlag");
		String searchContent = request.getParameter("searchContent").trim();
		List<Map<String, Object>> mapList = null;
		mapList = service.getMySearchRecord(whatFlag,searchContent);
		Gson gson = new Gson();
		String json = gson.toJson(mapList);
		// 遍历json数组
		JsonParser parser = new JsonParser();
		JsonElement jsonElement = parser.parse(json);
		JsonArray jsonArray = jsonElement.getAsJsonArray();
		for (int i = 0; i < jsonArray.size(); i++) {
			JsonObject jsonObject = jsonArray.get(i).getAsJsonObject();
			// 截取title，拼接
			String titleStr = "";
			if(jsonObject.get("title").getAsString().length()>8) {
				titleStr = jsonObject.get("title").getAsString().substring(0, 8)+"......";
			}else {
				titleStr = jsonObject.get("title").getAsString();
			}
			jsonObject.addProperty("titleStr", titleStr);
			// 截取detailinfo，拼接
			String detailinfoStr = "";
			if(jsonObject.get("detailinfo").getAsString().length()>12) {
				detailinfoStr = jsonObject.get("detailinfo").getAsString().substring(0, 12)+"......";
			}else {
				detailinfoStr = jsonObject.get("detailinfo").getAsString();
			}
			jsonObject.addProperty("detailinfoStr", detailinfoStr);
			// 截取issuetime，拼接
			String issuetimeStr = "";
			if(jsonObject.get("issuetime").getAsString().length()>10) {
				issuetimeStr = jsonObject.get("issuetime").getAsString().substring(0, 10);
			}else {
				issuetimeStr = jsonObject.get("issuetime").getAsString();
			}
			jsonObject.addProperty("issuetimeStr", issuetimeStr);
		}
		// 重新格式化json数据		
		String rows = gson.toJson(jsonElement);
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write("{\"articleList\":"+rows+"}");
	}
	
	// 弃用，2019/03/03
	// 确定捡到 或者 确定是失主
	public void handleForConfirmTakenArticle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String aid = request.getParameter("aid");
		Map<String, Object> article = null;
		article = service.handleForConfirmTakenArticle(aid);
		HttpSession session = request.getSession();
		session.setAttribute("article", article);
		//response.sendRedirect(request.getContextPath()+"/user/01/detail.jsp");
	}
}
	